package com.woyowodaows.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;


import com.woyowodaows.R;
import com.woyowodaows.adapter.mine.AddressListAdapter;
import com.woyowodaows.base.BaseActivity;
import com.woyowodaows.bean.mine.AddressInfoData;
import com.woyowodaows.bean.mine.AddressInfosBean;
import com.woyowodaows.request.HttpAsyncTask;
import com.woyowodaows.request.HttpRequestListener;
import com.woyowodaows.request.InterfaceUrl;
import com.woyowodaows.utils.LoginJudge;
import com.woyowodaows.utils.ToastUtil;
import com.woyowodaows.weight.LoadingDataLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 地址管理
 */

public class AddressManageActivity extends BaseActivity {

    @BindView(R.id.recycle_address)
    RecyclerView rvAddressList;
    @BindView(R.id.bt_add_address)
    Button addAds;
    String key;
    @BindView(R.id.rl_loading)
    LoadingDataLayout rlLoading;

    private List<AddressInfosBean> addressInfoList = new ArrayList<>();

    private AddressListAdapter addressRlAdapter;
    private final static int REQUESTCODE = 1; // 返回的结果码

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    if (addressInfoList != null && addressInfoList.size() > 0) {
                        initAddressManageData();
//                        ToastUtil.show(mContext,"设置成功！");
                    } else {
                        ToastUtil.show_short(AddressManageActivity.this, "没有数据！");
                        initAddressManageData();
                    }
                    if (addressInfoList.size() > 0){
                        rlLoading.setStatus(LoadingDataLayout.SUCCESS);
                    }else {
                        rlLoading.setStatus(LoadingDataLayout.EMPTY);
                    }
                    break;

            }
        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.activity_address_manager;
    }


    @Override
    public void initView() {
        super.initView();
        setTitleBar(false, "地址管理", null, 0, 0, null);
        ButterKnife.bind(this);
//        key = getIntent().getStringExtra("KEY");
    }

    @Override
    public void initData() {
        super.initData();
        addressInfoList.clear();
        rlLoading.setStatus(LoadingDataLayout.LOADING);
        getAddressData();

        rlLoading.setOnReloadListener(new LoadingDataLayout.OnReloadListener() {
            @Override
            public void onReload(View v, int status) {
                rlLoading.setStatus(LoadingDataLayout.LOADING);
                addressInfoList.clear();
                getAddressData();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void initAddressManageData() {
        //设置适配器
        rvAddressList.setLayoutManager(new LinearLayoutManager(this));
        if (addressRlAdapter == null) {
            addressRlAdapter = new AddressListAdapter(mContext, AddressManageActivity.this, addressInfoList, rvAddressList);
            rvAddressList.setAdapter(addressRlAdapter);
            addressRlAdapter.notifyDataSetChanged();
        }
//        addressRlAdapter.setOnItemClickListener(new AddressListAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(AddressInfosBean bean) {
//                if (TextUtils.equals(key, "1")) {
//                    EventBus.getDefault().post(new MessageEvent(AddressManageActivity.class.getSimpleName(), bean));
//                    finish();
//                } else if (TextUtils.equals(key, "2")) {
//                    Intent intent = new Intent(AddressManageActivity.this, SuperMarketComfirmOrderActivity.class);
//                    intent.putExtra(LoginJudge.DATA, bean);
//                    setResult(200, intent);
//                    finish();
//                }
//            }
//        });

        rlLoading.setOnReloadListener(new LoadingDataLayout.OnReloadListener() {
            @Override
            public void onReload(View v, int status) {
                rlLoading.setStatus(LoadingDataLayout.LOADING);
                isRefresh = true;
                getAddressData();
            }
        });
    }

    @OnClick(R.id.bt_add_address)
    public void onAdd() {
        Intent intent = new Intent(this, DetailedAddressActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("ATYPE", "addAdd");
        intent.putExtras(bundle);
        startActivityForResult(intent, REQUESTCODE); //REQUESTCODE--->1
    }

    private void getAddressData() {
        Map<String, Object> mparam = new HashMap<String, Object>();
        HttpAsyncTask.getInstance().onPostParam(mContext, "", false, InterfaceUrl.QUERYUSERADDRESS, AddressInfoData.class, mparam, new HttpRequestListener() {
            @Override
            public void onSuccess(Object result) {
                if (result != null) {
                    AddressInfoData bean = (AddressInfoData) result;
                    if (bean.getResult() == HttpAsyncTask.SUCCESS) {
                        maxPage = 0;
                        if (bean.getData()!= null && bean.getData().size() > 0) {
                            addressInfoList.addAll(bean.getData());
                            handler.sendEmptyMessage(0);
                        }
                    } else {
                        ToastUtil.show(mContext, bean.getMessage());
                    }
                    if (addressInfoList.size() > 0){
                        rlLoading.setStatus(LoadingDataLayout.SUCCESS);
                    }else {
                        rlLoading.setStatus(LoadingDataLayout.EMPTY);
                    }
                }

            }

            @Override
            public void onError(String message) {
                if ( addressInfoList !=null && addressInfoList.size() > 0){
                }else {
                    rlLoading.setStatus(LoadingDataLayout.ERROR);
                }
            }
        });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 202) {
            if (requestCode == REQUESTCODE) {
                addressInfoList.clear();
                rlLoading.setStatus(LoadingDataLayout.LOADING);
                getAddressData();
            }
        }
    }


}
